package com.aliyun.smartlight.service.impl;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.aliyun.smartlight.constants.ErrorCode;
import com.aliyun.smartlight.exception.AddDataException;
import com.aliyun.smartlight.exception.BaseException;
import com.aliyun.smartlight.exception.DeleteErrorException;
import com.aliyun.smartlight.exception.UpdateException;
import com.aliyun.smartlight.mapper.AreaMapper;
import com.aliyun.smartlight.pojo.Area;
import com.aliyun.smartlight.service.AreaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jackiechan on 2020/12/12 23:08
 *
 * @Author jackiechan
 */
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public void addArea(Area area) {
        if (area == null) {
            throw new AddDataException("数据不能为空", ErrorCode.NULL_DATA);
        }

        try {
            areaMapper.addArea(area);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddDataException("添加失败", ErrorCode.FAIL);
        }

    }

    @Override
    public void updateArea(Area area) {
        if (area == null || area.isNull()) {
            throw new AddDataException("数据不能为空", ErrorCode.NULL_DATA);
        }
        if ("0".equalsIgnoreCase(area.getStatus())) {//如果也要更新状态为禁用
            List<Area> areaList = getAreaByParentId(area.getId());//获取以当前区域 id 为父 id 的区域,如果有则不允许更新为禁用
            if (areaList.size() != 0) {
                throw new UpdateException("当前区域的子区域不为空,无法禁用", ErrorCode.DATA_NOT_EPTY);
            }
        }

        try {
            areaMapper.updateArea(area);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpdateException("更新失败", ErrorCode.FAIL);

        }
    }

    @Override
    public void deleteAreas(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new DeleteErrorException("没有传递 id", ErrorCode.NULL_DATA);
        }

        try {
            areaMapper.deleteAreas(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeleteErrorException("删除失败", ErrorCode.FAIL);
        }
    }

    @Override
    public Area getAreaById(Long id) {
        if (id == null || id == 0) {
            throw new BaseException("没有传递 id", ErrorCode.NULL_DATA);
        }

        try {
            Area area = areaMapper.getAreaById(id);
            if (area == null) {
                throw new BaseException("没有查询到结果", ErrorCode.FAIL);
            }
            return area;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("查询失败", ErrorCode.FAIL);
        }
    }

    @Override
    public List<Area> getAreaByParentId(Long id) {
        if (id == null || id == 0) {
            throw new BaseException("没有传递 id", ErrorCode.NULL_DATA);
        }

        try {
            List<Area> areaList = areaMapper.getAreaByParentId(id);
            return areaList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("查询失败", ErrorCode.FAIL);

        }
    }

    @Override
    public PageInfo<Area> getAllAreas(int pageNum, int pageSize, String exludeId, String areanname, int status) {
        PageHelper.startPage(pageNum, pageSize);
        try {
            List<Area> areaList = areaMapper.getAllAreas(exludeId, areanname, status);
            return new PageInfo<>(areaList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("查询失败", ErrorCode.FAIL);
        }
    }

    @Override
    public List<Area> getAllFirstParentAreas(int status) {

        try {
            return areaMapper.getAllFirstParentAreas(status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("查询异常", ErrorCode.FAIL);
        }
    }
}
